package com.ux.iot.jetlinks.service.sub.subscribe;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ux.iot.jetlinks.bo.JetlinkSub;
import com.ux.iot.jetlinks.bo.JetlinksUnsub;
import com.ux.iot.jetlinks.pro.ProductConfig;
import com.ux.iot.jetlinks.service.product.ProductService;
import com.ux.iot.jetlinks.service.sub.ProductLevelSub;
import com.ux.iot.jetlinks.service.sub.ProductSubEnum;
import com.ux.iot.jetlinks.service.sub.SubContant;
import com.ux.iot.jetlinks.service.sub.request.ServiceInfo;
import com.ux.iot.jetlinks.service.websocket.JetlinksConnectListener;
import com.ux.iot.jetlinks.service.websocket.SingleEndPointAsyncIotMsgService;
import com.ux.iot.jetlinks.util.JetlinksUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author caixiaodao
 * @version 1.0
 * 功能描述
 * 订阅
 * @date 2021/11/16 14:15
 */
@AllArgsConstructor
@Slf4j
public class SampleProductSubscribe  implements ProductLevelSub , JetlinksConnectListener<Session> {
    ProductService productService;
    ServiceInfo serviceInfo;
    SingleEndPointAsyncIotMsgService asyncIotMsgService;


    @Override
    public void sub(String productId, String deviceId, String topic, Map<String, Object> parameter) {
        List<ProductConfig> products= productService.getProductList();
        log.info("需配置的产品ID {}", JSON.toJSONString(products));
        products.forEach((item)->{
            String id=item.getProductId();
            //订阅在线
            if(StrUtil.isNotEmpty(item.getTopics())){
                List<String> topicList= Arrays.asList(item.getTopics().split(","));
                if(CollUtil.isNotEmpty(topicList)){
                    topicList.stream().forEach((sub)->{
                        asyncIotMsgService.send(JSON.toJSONString(getSubInfoByStr(sub,item.getProductId())));
                    });

                }
            }else{
                log.info("使用默认订阅Topic");
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.ONLINE,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.OFFLINE,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_EVENT_EVENTID,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_PROPERTY_REPORT,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_SEND_PROPERTY_READ,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_SEND_PROPERTY_WRITE,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_PROPERTY_READ_REPLY,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_PROPERTY_WRITE_REPLY,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_SEND_FUNCTION,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_FUNCTION_REPLY,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.REGISTER,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.UNREGISTER,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_CHILDREN_CHILDRENDEVICEID_TOPIC,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_CHILDREN_REPLY,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.MESSAGE_DIREC,id)));
                asyncIotMsgService.send(JSON.toJSONString(getSubInfo(ProductSubEnum.METADATA_DERIVED,id)));
            }


        });

    }

    @Override
    public void unsub(String productId, String deviceId, String topic) {
        List<ProductConfig> products= productService.getProductList();
        products.forEach((item)-> {
            String id=item.getProductId();
            if(StrUtil.isNotEmpty(item.getTopics())){
                List<String> topicList= Arrays.asList(item.getTopics().split(","));
                if(CollUtil.isNotEmpty(topicList)){
                    topicList.stream().forEach((sub)->{
                        asyncIotMsgService.send(JSON.toJSONString(getUnSubInfoByStr(sub,item.getProductId())));
                    });

                }
            }else {
                log.info("使用默认取消订阅Topic");
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.ONLINE, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.OFFLINE, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_EVENT_EVENTID, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_PROPERTY_REPORT, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_SEND_PROPERTY_READ, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_SEND_PROPERTY_WRITE, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_PROPERTY_READ_REPLY, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_PROPERTY_WRITE_REPLY, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_SEND_FUNCTION, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_FUNCTION_REPLY, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.REGISTER, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.UNREGISTER, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_CHILDREN_CHILDRENDEVICEID_TOPIC, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_CHILDREN_REPLY, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.MESSAGE_DIREC, id)));
                asyncIotMsgService.send(JSON.toJSONString(getUnSubInfo(ProductSubEnum.METADATA_DERIVED, id)));
            }
        });
    }


    public JetlinkSub getSubInfo(ProductSubEnum sub,String productId){
        JetlinkSub jetlinkSub= sub.getJetlinkSub();
        String prefix=SubContant.topic_Prefix;
        jetlinkSub.setTopic(prefix.replace("{productId}",productId).concat(sub.getTopic()));
        jetlinkSub.setParameter(null);
        jetlinkSub.setId(JetlinksUtil.bulidSubRequestId(serviceInfo,jetlinkSub.getTopic(),productId,""));
        return  jetlinkSub;
    }
    public JetlinkSub getSubInfoByStr(String topic,String productId){
        JetlinkSub jetlinkSub= JetlinkSub.builder().build();
        jetlinkSub.setTopic(topic.replace("{productId}",productId));
        jetlinkSub.setParameter(null);
        jetlinkSub.setId(JetlinksUtil.bulidSubRequestId(serviceInfo,jetlinkSub.getTopic(),productId,""));
        return  jetlinkSub;
    }

    public JetlinksUnsub getUnSubInfoByStr(String sub, String productId){
        JetlinkSub st=getSubInfoByStr(sub,productId);
        JetlinksUnsub unsub=JetlinksUnsub.builder().id(st.getId()).build();
        return  unsub;
    }

    public JetlinksUnsub getUnSubInfo(ProductSubEnum sub, String productId){
        JetlinkSub st=getSubInfo(sub,productId);
        JetlinksUnsub unsub=JetlinksUnsub.builder().id(st.getId()).build();
        return  unsub;
    }

    @Override
    public void connect(Session session) {
        sub(null,null,null,null);
    }

    @Override
    public void disconnect(Session session) {
        unsub(null,null,null);
    }


}
